﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tools
{
    /// <summary>
    /// 系统日志方法类
    /// </summary>
    public class TxtLogHelper
    {
        public void AddLogMsg(string contents)
        {
            #region 创建日志

            contents += "\r\n" + "HappenTime：" + DateTime.Now.ToString() + "\r\n";
            //生成目录
            //创建文件夹
            string fname = "";
            string path = GetFilePath(ref fname);
            var s = CheckFileSize(path + "/" + fname);
            string fullpath = path + "/" + fname;
            if (s > 8.5)
            {
                var split = fname.Split('.');
                FileInfo nf = new FileInfo(fullpath);
                nf.MoveTo(Path.Combine(path, split[0] + "-" + DateTime.Now.ToString("HHmmss") + "." + split[1]));
            }
            using (FileStream fs = new FileStream(fullpath, FileMode.Append, FileAccess.Write))
            {
                using (StreamWriter sr = new StreamWriter(fs))
                {
                    try
                    {
                        sr.WriteLine(contents);//开始写入值
                    }
                    catch (Exception ex)
                    {
                        sr.Close();
                        fs.Close();
                        return;
                    }
                }
            }
            #endregion
        }

        /// <summary>
        /// 创建文件夹以及日志文件
        /// </summary>
        /// <param name="fname"></param>
        /// <returns></returns>
        private static string GetFilePath(ref string fname)
        {
            string n = DateTime.Now.ToString("yyyyMMdd");
            string filePath = string.Empty;
            string ExcelPathFolder = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "sysLog";
            string filename = string.Empty;
            filename = "Log_" + n + ".txt";
            fname = filename;
            if (!Directory.Exists(ExcelPathFolder))  //如果Folder不存在，则创建
            {
                Directory.CreateDirectory(ExcelPathFolder);
            }
            filePath = System.IO.Path.Combine(ExcelPathFolder, filename);
            if (!System.IO.File.Exists(filePath))
            {
                FileStream fs1 = new FileStream(filePath, FileMode.Create, FileAccess.Write);//创建写入文件 

                fs1.Close();
            }
            return ExcelPathFolder;
        }
        /// <summary>
        /// 验证文件大小是否超出上限
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        private static long CheckFileSize(string filePath)
        {
            FileInfo f = new FileInfo(filePath);
            long size = 0;
            if (f.Exists)
            {
                size = f.Length / (1024 * 1024);
            }
            return size;
        }

    }
}
